package com.mai.demo.entity;

import io.minio.messages.Metadata;
import io.minio.messages.Owner;
import io.minio.messages.ResponseDate;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.time.ZonedDateTime;
import java.util.Map;

/**
 * @className: MinioItem
 * @author: Wang
 * @date: 2024/12/17 上午11:33
 * @Version: 1.0
 */
public class MinioItem {
    private static final String UTF_8 = StandardCharsets.UTF_8.toString();

    private String etag; // except DeleteMarker

    private String objectName;

    private ResponseDate lastModified;

    private Owner owner;

    private long size; // except DeleteMarker

    private String storageClass; // except DeleteMarker, not in case of MinIO server.

    private boolean isLatest; // except ListObjects V1

    private String versionId; // except ListObjects V1

    private Metadata userMetadata;

    private boolean isDir = false;
    private String encodingType = null;

    public MinioItem() {}

    /** Constructs a new Item for prefix i.e. directory. */
    public MinioItem(String prefix) {
        this.objectName = prefix;
        this.isDir = true;
    }

    public String getEtag() {
        return etag;
    }

    public void setEtag(String etag) {
        this.etag = etag;
    }


    public void setObjectName(String objectName) {
        this.objectName = objectName;
    }



    public void setLastModified(ResponseDate lastModified) {
        this.lastModified = lastModified;
    }

    public Owner getOwner() {
        return owner;
    }

    public void setOwner(Owner owner) {
        this.owner = owner;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public String getStorageClass() {
        return storageClass;
    }

    public void setStorageClass(String storageClass) {
        this.storageClass = storageClass;
    }

    public void setLatest(boolean latest) {
        isLatest = latest;
    }



    public void setVersionId(String versionId) {
        this.versionId = versionId;
    }


    public void setUserMetadata(Metadata userMetadata) {
        this.userMetadata = userMetadata;
    }

    public void setDir(boolean dir) {
        isDir = dir;
    }

    public String getEncodingType() {
        return encodingType;
    }

    public void setEncodingType(String encodingType) {
        this.encodingType = encodingType;
    }

    /** Returns object name. */
    public String getObjectName() {
        try {
            return "url".equals(encodingType) ? URLDecoder.decode(objectName, UTF_8) : objectName;
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    /** Returns last modified time of the object. */
    public ZonedDateTime getLastModified() {
        return this.lastModified == null ? null : lastModified.zonedDateTime();
    }

    /** Returns user metadata. This is MinIO specific extension to ListObjectsV2. */
    public Map<String, String> getUserMetadata() {
        return (userMetadata == null) ? null : userMetadata.get();
    }

    /** Returns whether this version ID is latest. */
    public boolean isLatest() {
        return isLatest;
    }

    /** Returns version ID. */
    public String getVersionId() {
        return versionId;
    }

    /** Returns whether this item is a directory or not. */
    public boolean isDir() {
        return isDir;
    }

    /** Returns whether this item is a delete marker or not. */
//    public boolean isDeleteMarker() {
//        return this instanceof DeleteMarker;
//    }
}
